package com.a214.yxz.yuejian013.Fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Activity.AboutUsActivity;
import com.a214.yxz.yuejian013.Activity.UserFollowActivity;
import com.a214.yxz.yuejian013.Activity.UserOrderActivity;
import com.a214.yxz.yuejian013.Activity.UserPostActivity;
import com.a214.yxz.yuejian013.Activity.UserSettingActivity;
import com.a214.yxz.yuejian013.Base.BaseSFragment;
import com.a214.yxz.yuejian013.JavaBean.UserBean;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;
import com.squareup.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends BaseSFragment {

    private ImageView userPic;
    private TextView userPost,userFollow,userOrder,userID;
    private Button aboutUs,me,scan;
    private ServiceListener<UserBean> listener;
    private ResModel model=new ResModel("gson",getSessionID());


    public UserFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        userPic= (ImageView) view.findViewById(R.id.user_userPic);
        userPost= (TextView) view.findViewById(R.id.user_userPost);
        userFollow= (TextView) view.findViewById(R.id.user_userFollow);
        userOrder= (TextView) view.findViewById(R.id.user_userOrder);
        userID= (TextView) view.findViewById(R.id.user_name);
        aboutUs= (Button) view.findViewById(R.id.user_aboutUs);
        me= (Button) view.findViewById(R.id.user_me);
        scan= (Button) view.findViewById(R.id.user_scan);

        listener=new ServiceListener<UserBean>() {
            @Override
            public void onResponse(UserBean l) {
                userPic.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.user));
                userID.setText(l.getUser_name());
                Picasso.with(getContext())
                        .load("http://172.24.10.175/workout/Uploads/" + l.getPhoto())
                        .placeholder(R.drawable.user)
                        .into(userPic);
                userID.setText(l.getUser_name());

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        };


        userPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),UserPostActivity.class);
                getContext().startActivity(intent);
            }
        });
        userFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),UserFollowActivity.class);
                getContext().startActivity(intent);
            }
        });
        userOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),UserOrderActivity.class);
                getContext().startActivity(intent);
            }
        });
        aboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), AboutUsActivity.class);
                getContext().startActivity(intent);
            }
        });
        me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),UserSettingActivity.class);
                getActivity().startActivityForResult(intent,1);
            }
        });
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
            }
        });
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        Log.e("uid",sharedPreferences.getString("user_id","408"));
        model.getMine(sharedPreferences.getString("user_id","408"),listener);
    }



    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        Log.e("uid",sharedPreferences.getString("user_id","408"));
        model.getMine(sharedPreferences.getString("user_id","408"),listener);
    }
}
